package models.user;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import play.Logger;
import play.db.ebean.Model;
import utils.MengException;

/**
 * 用户工作经历
 * @author zhangpeng
 *
 */

@Entity
@Table(name="work_user_info")
public class WorkUserInfo extends Model{

	private static final long serialVersionUID = 1L;

	@Id
	public long id;
	public String username;
	public String company;
	/**
	 * 职业
	 */
	public String profession;
	/**
	 * 职位
	 */
	public String job;
	/**
	 * 年收入 
	 */
	public String income;
	
	public static WorkUserInfo getInstances(){
		return new WorkUserInfo();
	}
	
	public static Model.Finder<Long, WorkUserInfo> find = new Model.Finder<Long, WorkUserInfo>(Long.class, WorkUserInfo.class);
	
	/**
	 * 创建用户工作经历
	 * 
	 * @param eui
	 * @return
	 * @throws MengException
	 */
	public boolean createWUI(WorkUserInfo eui) throws MengException {

		if (UserInfo.getInstances().verifyUsername(eui.username)) {
			throw new MengException("500001", "用户名不存在");
		}

		if (verifyWorkUserInfo(eui.username)) {
			throw new MengException("500002", "用户工作经历已存在");
		}

		try {
			eui.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createWUI Exception," + t.getMessage());
			throw new MengException("500003", "用户工作经历保存失败");
		}
	}

	public boolean createWUI(UserInfo ui) throws MengException {

		if (verifyWorkUserInfo(ui.username)) {
			throw new MengException("500008", "用户基本信息已存在");
		}
		WorkUserInfo eui = getInstances();
		eui.username = ui.username;
		try {
			eui.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createBUI Exception," + t.getMessage());
			throw new MengException("500009", "用户基本信息保存失败");
		}
	}

	/**
	 * 用户工作经历修改
	 * 
	 * @param eui
	 * @return
	 * @throws MengException
	 */
	public boolean modifyWUI(WorkUserInfo eui) throws MengException {

		if (UserInfo.getInstances().verifyUsername(eui.username)) {
			throw new MengException("500004", "用户名不存在");
		}
		eui.id = getWUI(eui.username).id;

		try {
			eui.update();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo modifyWUI Exception," + t.getMessage());
			throw new MengException("500005", "用户工作经历修改失败");
		}
	}

	/**
	 * 获取用户工作经历
	 * 
	 * @param username
	 * @return
	 * @throws MengException
	 */
	public WorkUserInfo getWUI(String username) throws MengException {

		if (UserInfo.getInstances().verifyUsername(username)) {
			throw new MengException("500006", "用户名不存在");
		}

		try {

			return find.where().eq("username", username).findUnique();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo getWUI Exception," + t.getMessage());
			throw new MengException("500007", "获取用户工作经历失败");
		}
	}
	
	public boolean verifyWorkUserInfo(String username) throws MengException {

		if (getWUI(username) != null) {
			return true;
		}

		return false;
	}
	

}
